﻿using System;

namespace ReactiveGraph.Extensions.Rules.Core
{
    public class RuleBase<TContext>  : IDeclaration<TContext>, IAction<TContext>, IDeclared<TContext> 
    {
        private Action<TContext> _action;
        private Func<TContext, bool> _canExecute;

     

        IAction<TContext> IDeclaration<TContext>.On(Func<TContext, bool> predicate)
        {
            _canExecute = predicate;
            return this;
        }

        IDeclared<TContext> IAction<TContext>.Action(Action<TContext> action)
        {
            _action = action;
            return this;
        }

        public IDeclared<TContext> Action(Action action)
        {
            _action = context => action();
            return this;
        }

        void IDeclared<TContext>.Execute(TContext context)
        {
            if(_canExecute(context))
                _action(context);
        }
    }
}
